﻿/**************************************************************
*作者：Leon
*创建时间：2022/4/5 20:22:58
**************************************************************/
using Microsoft.Toolkit.Mvvm.ComponentModel;
using Microsoft.Toolkit.Mvvm.Input;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;

namespace Lad.DrawProject.Controls;

public enum FontCase
{
    None,
    Upper,
    Lower,
}
public enum HorizontalVerticalAlignment
{
    TopAlignLeft,
    TopAlignCenter,
    TopAlignRight,
    TopAlignJustify,
    CenterAlignLeft,
    CenterAlignCenter,
    CenterAlignRight,
    CenterAlignJustify,
    BottomAlignLeft,
    BottomAlignCenter,
    BottomAlignRight,
    BottomAlignJustify,
}
public interface IFontViewModel : INotifyPropertyChanged
{
    /// <summary>
    /// 字体集
    /// </summary>
    string FontFamily { get; set; }

    /// <summary>
    /// 字体颜色
    /// </summary>
    Color FontColor { get; set; }

    /// <summary>
    /// 字体尺寸
    /// </summary>
    double FontSize { get; set; }

    /// <summary>
    /// 字体阴影颜色
    /// </summary>
    Color TextEffectColor { get; set; }

    /// <summary>
    /// 高亮颜色
    /// </summary>
    Color HighlightColor { get; set; }

    /// <summary>
    /// 字体方案
    /// </summary>
    FontCase FontCase { get; set; }

    /// <summary>
    /// 字体高度
    /// </summary>
    FontWeight FontWeight { get; set; }

    /// <summary>
    /// 字体样式
    /// </summary>
    FontStyle FontStyle { get; set; }

    /// <summary>
    /// 字体拉神
    /// </summary>
    FontStretch FontStretch { get; set; }

    /// <summary>
    /// 下划线
    /// </summary>
    bool Underline { get; set; }

    /// <summary>
    /// 删除线
    /// </summary>
    bool Strikethrough { get; set; }


    bool OverLine { get; set; }
    HorizontalAlignment HorizontalAlignment { get; set; }
    VerticalAlignment VerticalAlignment { get; set; }
    double LineHeight { get; set; }

}

public class FontViewModel : ObservableObject, IFontViewModel
{
    #region 界面使用
    static FontViewModel()
    {
        var systemFontFamilies = new List<FontFamily>();
        foreach (FontFamily _f in Fonts.SystemFontFamilies)
        {
            LanguageSpecificStringDictionary _fontDic = _f.FamilyNames;
            if (_fontDic.ContainsKey(XmlLanguage.GetLanguage("zh-cn")))
            {
                if (_fontDic.TryGetValue(XmlLanguage.GetLanguage("zh-cn"), out string _fontName))
                {
                    systemFontFamilies.Add(new FontFamily(_fontName));
                }
            }
            else
            {
                if (_fontDic.TryGetValue(XmlLanguage.GetLanguage("en-us"), out string _fontName))
                {
                    systemFontFamilies.Add(new FontFamily(_fontName));
                }
            }
        }

        FontFamilys = systemFontFamilies.Select(fontFamily => fontFamily.ToString()).ToArray();
    }
    public static string[] FontFamilys { get; }
    public static double[] FontSizes { get; } = new double[] { 7, 8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 28, 32, 36, 48, 72 };
    public static FontCase[] FontCases { get; } = new FontCase[] { FontCase.None, FontCase.Upper, FontCase.Lower };
    public static Color[] FontColors { get; } = new Color[] { Colors.Red, Colors.Green, Colors.Blue, Colors.White, Colors.Black, Colors.Purple };

    public ICommand GrowFontCommand
    {
        get
        {
            return new RelayCommand<object>(para =>
            {
                if (FontSize < 72)
                    FontSize++;
            });
        }
    }

    public ICommand ShrinkFontCommand
    {
        get
        {
            return new RelayCommand(() =>
            {
                if (FontSize > 1)
                    FontSize--;
            });
        }
    }

    public ICommand ClearFormattingCommand
    {
        get
        {
            return new RelayCommand(() =>
            {
                FontFamily = "Arial";
                FontSize = 12;
                FontColor = Colors.Black;
                FontWeight = FontWeights.Regular;
                FontStyle = FontStyles.Normal;
                FontStretch = FontStretches.Normal;
                Underline = false;
                Strikethrough = false;
                OverLine = false;
                TextEffectColor = Colors.Transparent;
                HighlightColor = Colors.Transparent;
                FontCase = FontCase.None;
                HorizontalAlignment = HorizontalAlignment.Center;
                VerticalAlignment = VerticalAlignment.Center;
                LineHeight = 0;
            });
        }
    }

    public ICommand TextEffectColorCommand
    {
        get
        {
            return new RelayCommand<Color>(para =>
            {
                TextEffectColor = para;
            });
        }
    }

    public ICommand HighlightColorCommand
    {
        get
        {
            return new RelayCommand<Color>(para =>
            {
                HighlightColor = para;
            });
        }
    }

    public ICommand FontColorCommand
    {
        get
        {
            return new RelayCommand<Color>(para =>
            {
                FontColor = para;
            });
        }
    }

    private HorizontalVerticalAlignment _horizontalVerticalAlignment = HorizontalVerticalAlignment.CenterAlignCenter;
    public HorizontalVerticalAlignment HorizontalVerticalAlignment
    {
        get
        {
            return _horizontalVerticalAlignment;
        }
        set
        {
            if (SetProperty(ref _horizontalVerticalAlignment, value))
            {
                _horizontalVerticalAlignment = value;
                switch (value)
                {
                    case HorizontalVerticalAlignment.TopAlignLeft:
                        HorizontalAlignment = HorizontalAlignment.Left;
                        VerticalAlignment = VerticalAlignment.Top;
                        break;
                    case HorizontalVerticalAlignment.TopAlignCenter:
                        HorizontalAlignment = HorizontalAlignment.Center;
                        VerticalAlignment = VerticalAlignment.Top;
                        break;
                    case HorizontalVerticalAlignment.TopAlignRight:
                        HorizontalAlignment = HorizontalAlignment.Right;
                        VerticalAlignment = VerticalAlignment.Top;
                        break;
                    case HorizontalVerticalAlignment.TopAlignJustify:
                        HorizontalAlignment = HorizontalAlignment.Stretch;
                        VerticalAlignment = VerticalAlignment.Top;
                        break;

                    case HorizontalVerticalAlignment.CenterAlignLeft:
                        HorizontalAlignment = HorizontalAlignment.Left;
                        VerticalAlignment = VerticalAlignment.Center;
                        break;
                    case HorizontalVerticalAlignment.CenterAlignCenter:
                        HorizontalAlignment = HorizontalAlignment.Center;
                        VerticalAlignment = VerticalAlignment.Center;
                        break;
                    case HorizontalVerticalAlignment.CenterAlignRight:
                        HorizontalAlignment = HorizontalAlignment.Right;
                        VerticalAlignment = VerticalAlignment.Center;
                        break;
                    case HorizontalVerticalAlignment.CenterAlignJustify:
                        HorizontalAlignment = HorizontalAlignment.Stretch;
                        VerticalAlignment = VerticalAlignment.Center;
                        break;

                    case HorizontalVerticalAlignment.BottomAlignLeft:
                        HorizontalAlignment = HorizontalAlignment.Left;
                        VerticalAlignment = VerticalAlignment.Bottom;
                        break;
                    case HorizontalVerticalAlignment.BottomAlignCenter:
                        HorizontalAlignment = HorizontalAlignment.Center;
                        VerticalAlignment = VerticalAlignment.Bottom;
                        break;
                    case HorizontalVerticalAlignment.BottomAlignRight:
                        HorizontalAlignment = HorizontalAlignment.Right;
                        VerticalAlignment = VerticalAlignment.Bottom;
                        break;
                    case HorizontalVerticalAlignment.BottomAlignJustify:
                        HorizontalAlignment = HorizontalAlignment.Stretch;
                        VerticalAlignment = VerticalAlignment.Bottom;
                        break;
                }
            }
        }
    }
    #endregion

    private string _fontFamily = "Arial";
    public string FontFamily
    {
        get
        {
            return _fontFamily;
        }
        set
        {
            if (!SetProperty(ref _fontFamily, value))
            {
                OnPropertyChanged(nameof(FontFamily));
            }
        }
    }

    private double _fontSize = 12;
    public double FontSize
    {
        get
        {
            return _fontSize;
        }
        set
        {
            if (!SetProperty(ref _fontSize, value))
            {
                OnPropertyChanged(nameof(FontSize));
            }

        }
    }

    private Color _fontColor = Colors.Black;
    public Color FontColor
    {
        get
        {
            return _fontColor;
        }
        set
        {
            if (!SetProperty(ref _fontColor, value))
            {
                OnPropertyChanged(nameof(FontColor));
            }
        }
    }

    private FontWeight _fontWeight = FontWeights.Regular;

    public FontWeight FontWeight
    {
        get
        {
            return _fontWeight;
        }
        set
        {
            if (!SetProperty(ref _fontWeight, value))
            {
                OnPropertyChanged(nameof(FontWeight));
            }
        }
    }

    private FontStyle _fontStyle = FontStyles.Normal;
    public FontStyle FontStyle
    {
        get
        {
            return _fontStyle;
        }
        set
        {
            if (!SetProperty(ref _fontStyle, value))
            {
                OnPropertyChanged(nameof(FontStyle));
            }
        }
    }

    private FontStretch _fontStretch = FontStretches.Normal;

    public FontStretch FontStretch
    {
        get
        {
            return _fontStretch;
        }
        set
        {
            if (!SetProperty(ref _fontStretch, value))
            {
                OnPropertyChanged(nameof(FontStretch));
            }
        }
    }

    private bool _underline;
    public bool Underline
    {
        get
        {
            return _underline;
        }
        set
        {
            if (SetProperty(ref _underline, value))
            {
                OnPropertyChanged(nameof(TextDecorations));
            }
            else
            {
                OnPropertyChanged(nameof(Underline));
            }
        }
    }

    private bool _strikethrough;
    public bool Strikethrough
    {
        get
        {
            return _strikethrough;
        }
        set
        {
            if (SetProperty(ref _strikethrough, value))
            {
                OnPropertyChanged(nameof(TextDecorations));
            }
            else
            {
                OnPropertyChanged(nameof(Strikethrough));
            }
        }
    }

    private bool _overLine;
    public bool OverLine
    {
        get
        {
            return _overLine;
        }
        set
        {
            if (SetProperty(ref _overLine, value))
            {
                OnPropertyChanged(nameof(TextDecorations));
            }
            else
            {
                OnPropertyChanged(nameof(OverLine));
            }
        }
    }

    private TextDecorationCollection? _textDecorations;
    /// <summary>
    /// 文字装饰
    /// </summary>
    public TextDecorationCollection TextDecorations
    {
        get
        {
            _textDecorations = new TextDecorationCollection();
            if (Underline)
            {
                _textDecorations.Add(System.Windows.TextDecorations.Underline);
            }
            if (Strikethrough)
            {
                _textDecorations.Add(System.Windows.TextDecorations.Strikethrough);
            }
            if (OverLine)
            {
                _textDecorations.Add(System.Windows.TextDecorations.OverLine);
            }
            return _textDecorations;
        }
        //set
        //{               
        //    NotifyChanged(nameof(TextDecorations));
        //}
    }

    private Color _textEffectColor = Colors.Transparent;
    public Color TextEffectColor
    {
        get { return _textEffectColor; }

        set
        {
            if (!SetProperty(ref _textEffectColor, value))
            {
                OnPropertyChanged(nameof(TextEffectColor));
            }
        }
    }

    private Color _highlightColor = Colors.Transparent;
    public Color HighlightColor
    {
        get { return _highlightColor; }

        set
        {
            if (!SetProperty(ref _highlightColor, value))
            {
                OnPropertyChanged(nameof(HighlightColor));
            }
        }
    }

    private FontCase _fontCase = FontCase.None;
    public FontCase FontCase
    {
        get
        {
            return _fontCase;
        }
        set
        {
            if (!SetProperty(ref _fontCase, value))
            {
                OnPropertyChanged(nameof(FontCase));
            }
        }
    }

    private HorizontalAlignment _horizontalAlignment = HorizontalAlignment.Center;
    public HorizontalAlignment HorizontalAlignment
    {
        get
        {
            return _horizontalAlignment;
        }
        set
        {
            if (!SetProperty(ref _horizontalAlignment, value))
            {
                OnPropertyChanged(nameof(HorizontalAlignment));
            }
        }
    }

    private VerticalAlignment _verticalAlignment = VerticalAlignment.Center;
    public VerticalAlignment VerticalAlignment
    {
        get
        {
            return _verticalAlignment;
        }
        set
        {
            if (!SetProperty(ref _verticalAlignment, value))
            {
                OnPropertyChanged(nameof(VerticalAlignment));
            }
        }
    }

    private double _lineHeight;
    public double LineHeight
    {
        get
        {
            return _lineHeight;
        }
        set
        {
            if (!SetProperty(ref _lineHeight, value))
            {
                OnPropertyChanged(nameof(LineHeight));
            }
        }
    }

}
